package com.yu.boot2.oss;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import com.aliyun.oss.model.SimplifiedObjectMeta;
import com.yu.boot2.config.OssConfigProperties;
import com.yu.boot2.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author zy
 * @date 2019-05-27 14:41
 **/
@Slf4j
@Service
public class OssService{

    @Autowired
    private OssConfigProperties ossConfigProperties;

    private OSSClient client;

    @PostConstruct
    private void initClient(){
        this.client = new OSSClient(ossConfigProperties.getEndPoint(), ossConfigProperties.getAccesskeyid(),
                ossConfigProperties.getAccesskeysecret());
    }

    public SimplifiedObjectMeta getInfo(String objectName){
        // 获取文件的部分元信息。
        SimplifiedObjectMeta objectMeta = client.getSimplifiedObjectMeta(ossConfigProperties.getBucketName(), objectName);
        System.out.println(objectMeta.getSize());
        System.out.println(objectMeta.getETag());
        System.out.println(objectMeta.getLastModified());
        // 关闭OSSClient。
        client.shutdown();
        return objectMeta;
    }

    public List<OSSObjectSummary> getList(String keyPrefix){
        // 列举文件。 如果不设置KeyPrefix，则列举存储空间下所有的文件。KeyPrefix，则列举包含指定前缀的文件。
        ObjectListing objectListing = client.listObjects(ossConfigProperties.getBucketName(), keyPrefix);
        List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
        for (OSSObjectSummary s : sums) {
            System.out.println("\t" + s.getKey());
        }

        // 关闭OSSClient。
        client.shutdown();
        return sums;
    }

    private String upload(InputStream inputStream, String path) {
        try {
            client.putObject(ossConfigProperties.getBucketName(), path, inputStream);
            client.shutdown();
        } catch (Exception e){
            log.error("上传阿里云服务异常",e);
        }
        return ossConfigProperties.getDomain() + "/" + path;
    }

    private String upload(byte[] data, String path) {
        return upload(new ByteArrayInputStream(data), path);
    }

    public String uploadSuffix(byte[] data, String suffix) {
        return upload(data, getPath(ossConfigProperties.getAliyunPrefix(), suffix));
    }

    public String uploadSuffix(InputStream inputStream, String suffix) {
        return upload(inputStream, getPath(ossConfigProperties.getAliyunPrefix(), suffix));
    }

    /**
     * 文件路径
     * @param prefix 前缀
     * @param suffix 后缀
     * @return 返回上传路径
     */
    private String getPath(String prefix, String suffix) {
        //生成uuid
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        //文件路径
        String path = DateUtils.format(new Date(), "yyyyMMdd") + "/" + uuid;

        if(StringUtils.isNotBlank(prefix)){
            path = prefix + "/" + path;
        }

        return path + "." + suffix;
    }

}
